#include "addfriendsearch.h"
#include "ui_addfriendsearch.h"
#include "user.h"
#include "showinfo.h"
#include <QHostAddress>
#include <QMessageBox>

addfriendsearch::addfriendsearch(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::addfriendsearch)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
}

void addfriendsearch::on_btn_send_addfriendsearch_clicked()
{
    //Sender创建
    UserBrief Sender;//创造一个UserBrief
    Sender = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    QString senderToken = userSelf.getUserPassword();
    QString receiverInfo=ui->receiverInfo->text();
    int idOrName=1;//1为id 2为name 待radio完成，默认为id
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/search_friend"));//连接http协议
    QJsonObject json2;
    //应该有radio button未实现
    if(idOrName==1)//如果为id
    {
        json2={
            {"search_type_code",idOrName},
            {"id",receiverInfo.toInt()},
            {"username",""},//username暂时为空
            {"self_id", Sender.id.toInt()},
            {"token",senderToken}
        };
        qDebug()<<"data"<<json2;
    }
    else
    {
        json2={
            {"search_type_code",idOrName},
            {"id",0},
            {"username",receiverInfo},
            {"self_id", Sender.id.toInt()},
            {"token",senderToken}
        };
        qDebug()<<"data"<<json2;
    }
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");//创建回复连接
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    //response为弹窗形式
    connect(reply, &QNetworkReply::finished, [=]()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 读取响应数据
            QByteArray dataReply = reply->readAll();
            // 将 JSON 数据转换为 QVariant 对象
            QJsonParseError error;
            QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
            //qDebug()<<result;
            // 处理 JSON 数据
            if(error.error == QJsonParseError::NoError)
            {
                QVariantMap resultMap = result.toVariantMap();
                //提取状态码
                QVariant status_code = resultMap.value("status_code");
                status_code.toString();
                if(status_code == "2")
                {
                    QVariant is_friend_code = resultMap.value("is_friend_code");

                    is_friend_code.toString();
                    if(is_friend_code=="0")
                    {
                        QMessageBox::information(this,"查询结果","你们还不是好友，速去添加~");
                        qDebug()<<resultMap;
                        //QMap senderMap = resultMap.value("user").toMap();
                        showinfo *si=new showinfo(nullptr,userSelf,resultMap);
                        si->show();
                    }
                    else
                        QMessageBox::information(this,"查询结果","你们已经是好友哩~");
                }
                else if(status_code == "1")
                    QMessageBox::information(this,"查询失败","不存在该用户！");
                //服务器出问题
                else
                    QMessageBox::information(this,"查询失败","好像哪里出了问题呢？肯定不是服务器的问题！");

            }
        }
        else
            qDebug()<<"error";
        reply->deleteLater();
    });
    this->close();
}


void addfriendsearch::on_btn_clear_addfriendsearch_clicked()
{
    //ui->messageSendTextEdit->clear();
    ui->receiverInfo->clear();
}

addfriendsearch::~addfriendsearch()
{
    delete ui;
}
